package com.thx.user.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.thx.common.struts2.BaseAction;
import com.thx.common.web.ActionResult;
import com.thx.log.LogUtil;
import com.thx.user.util.ModuleConstant;

/**
 * @author balance
 *
 */
@Namespace("/")
@Action(value = "logout", results = {
		@Result(name = "syn", params={"actionName","login","method","input","namespace","/"},type="chain"),
		@Result(name = "asyn", type = "json", params = { "root", "result" })
})
@Controller
@Scope("prototype")
public class LogoutAction extends BaseAction {

	private static final long serialVersionUID = 7894591642192988518L;

	public String execute() {
		return syn();
	}

	/**
	 * 同步退出
	 *
	 * @return
	 */
	public String syn() {
		destroy();
		return "syn";
	}

	/**
	 * 异步退出
	 *
	 * @return
	 */
	public String asyn() {
		destroy();
		this.setResult(ActionResult.SUCCESS, "已退出系统！");
		return "asyn";
	}

	private void destroy() {
		LogUtil.log(ModuleConstant.SYSTEM_MANAGER, LogUtil.ACTION_LOGOUT, LogUtil.RESULT_SUCCESS, "退出系统");
		ActionContext.getContext().getSession().clear();
		//getSession().invalidate();//2016-8-22贾红磊注释掉了，因为执行此句后，通过chain跳转到另外一个action时会报session invalidate异常
	}

}
